package top.hkyzf.study.leetcode.linkedlist;

import org.junit.Test;

/**
 * @author 朱峰
 * @date 2022-1-23 16:14
 */
public class 链表的中间结点_876 {
    public ListNode middleNode(ListNode head) {
        ListNode old = head;
        int index = 0;
        while (head != null) {
            index++;
            head = head.next;
        }
        int middle = index / 2 + 1;
        head = old;
        for (int i = 1; i < middle; i++) {
            head = head.next;
        }
        return head;
    }

    @Test
    public void testMiddleNode () {
        ListNode node6 = new ListNode(6);
        ListNode node5 = new ListNode(5, node6);
        ListNode node4 = new ListNode(4, node5);
        ListNode node3 = new ListNode(3, node4);
        ListNode node2 = new ListNode(2, node3);
        ListNode head = new ListNode(1, node2);
        System.out.println(middleNode(head));
    }
}
